package com.pktechan.manage.controller;

import com.pktechan.manage.base.annotation.Auth;
import com.pktechan.manage.entity.SysOperate;
import com.pktechan.manage.entity.SysRole;
import com.pktechan.manage.entity.SysUser;
import com.pktechan.manage.exception.CustomException;
import com.pktechan.manage.service.SysRoleService;
import com.pktechan.manage.utils.HtmlUtil;
import com.pktechan.manage.vo.RoleOperateListVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: s.zhangbj03307
 * @create: 2021/3/22 17:11
 **/
@Controller
@RequestMapping("sysRole")
public class SysRoleController {

    private static final Logger logger = LoggerFactory.getLogger(StatusFlowController.class);

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 打开角色列表页面
     * @return
     */
    @RequestMapping("toUserRolesList")
    public String toUserRolesList() {
        return "role/roleList";
    }
    /**
     * 打开添加角色页面
     * @return
     */
    @RequestMapping("toAddRole")
    public String toAddRole() {
        return "role/roleAdd";
    }
    /**
     * 打开查询详情页面
     * @return
     */
    @RequestMapping("toRoleQuery")
    public String toRoleQuery() {
        return "role/roleQuery";
    }
    /**
     * 打开修改角色页面
     * @return
     */
    @RequestMapping("toUpdateRole")
    public String toUpdateRole() {
        return "role/roleUpdate";
    }
    /**
     * 获取角色信息列表
     * @param sysRole
     * @param request
     * @param response
     */
    @RequestMapping(value = "/userRolesList", method = RequestMethod.POST)
    public void getRoleByFilter(@RequestBody SysRole sysRole, HttpServletRequest request,
                                HttpServletResponse response) {
        try {
            logger.info("================== sysUser query ==================");

            List<SysRole> userRoles = sysRoleService.getList(sysRole);
            HtmlUtil.writerSuccessJson(response, userRoles);
        } catch (Exception e) {
            HtmlUtil.writerFailJson(response, e.getMessage());
            logger.error(e.getMessage(), e);
        }
    }
    /**
     * 删除指定角色基本信息及关联关系。
     *
     * @param sysRole
     * TA清算
     * @param response
     */
    @RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
    @Auth(verifyOperateKey = "roleMng")
    public void deleteRole(@RequestBody SysRole sysRole ,HttpServletRequest request,
                           HttpServletResponse response) {
        try {
            logger.info("================== sysRole delete ==================");

            Integer id = sysRole.getId();
            if (id == null) {
                throw new Exception("删除失败，角色唯一标识不能为空。");
            }
            sysRoleService.deleteSystemRole(id);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException eme) {
            HtmlUtil.writerFailJson(response, eme.getMessage());
            logger.debug(eme.getMessage());
        } catch (Exception e) {
            HtmlUtil.writerFailJson(response, e.getMessage());
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 添加角色信息列表。
     *
     * @param roleOperateListEntity
     * TA清算
     * @param response
     */
    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    @Auth(verifyOperateKey = "roleMng")
    public void addAndBindingOperate(@RequestBody RoleOperateListVO roleOperateListEntity,
                                     HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("================== sysRole add ==================");
//            SysUser sysUser = SessionUtils.getUser(request);
//            roleOperateListEntity.getRole().setCreator(sysUser.getId());
            SysRole flag= sysRoleService.addAndBindingOperate(roleOperateListEntity);
            HtmlUtil.writerSuccessJson(response,flag);
        } catch (CustomException eme) {
            HtmlUtil.writerFailJson(response, eme.getMessage());
            logger.debug(eme.getMessage());
        } catch (Exception e) {
            HtmlUtil.writerFailJson(response, e.getMessage());
            logger.error(e.getMessage(), e);
        }
    }

    /**
     * 查询全部权限
     *
     *TA清算
     * @param request
     * @param response
     */
    @RequestMapping(value = "/allOperate", method = RequestMethod.GET)
    public void allOperate(HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("================== sysRole query ==================");
            List<SysOperate> sysOperateList = sysRoleService.queryOperateListByRoleId();
            HtmlUtil.writerSuccessJson(response, sysOperateList);
        } catch (CustomException eme) {
            HtmlUtil.writeFallJsonByKey(request, response, eme.getMessage(), null);
            logger.debug(eme.getMessage());
        } catch (Exception e) {
            HtmlUtil.writerFailJson(response, e.getMessage());
            logger.error(e.getMessage(), e);
        }
    }
    /**
     * 查看角色相关员工列表。
     *
     * @param sysRole
     * TA清算
     * @param response
     */
    @RequestMapping(value = "/getOaStaffsByRoleId", method = RequestMethod.POST)
    @Auth(verifyOperateKey = "roleMng")
    public void getOaStaffsByRoleId(@RequestBody SysRole sysRole,
                                    HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("================== sysRole query ==================");
            List<SysUser> systemUserEntityList= sysRoleService.getOaStaffsByRoleId(sysRole);
            HtmlUtil.writerSuccessJson(response,systemUserEntityList);
        } catch (CustomException eme) {
            HtmlUtil.writerFailJson(response, eme.getMessage());
            logger.debug(eme.getMessage());
        } catch (Exception e) {
            HtmlUtil.writerFailJson(response, e.getMessage());
            logger.error(e.getMessage(), e);
        }
    }
    /**
     * 更新角色信息列表。
     *
     * @param roleOperateListEntity
     * TA清算
     * @param request
     * @param response
     */
    @RequestMapping(value = "/updateRole", method = RequestMethod.POST)
    @Auth(verifyOperateKey = "roleMng")
    public void saveRoleById(@RequestBody RoleOperateListVO roleOperateListEntity, HttpServletRequest request,
                             HttpServletResponse response) {
        try {
            logger.info("================== sysRole update ==================");
            sysRoleService.updateSystemRole(roleOperateListEntity);
            HtmlUtil.writerSuccessJson(response);
        } catch (CustomException eme) {
            HtmlUtil.writeFallJsonByKey(request, response, eme.getMessage(), null);
            logger.debug(eme.getMessage());
        } catch (Exception e) {
            HtmlUtil.writerFailJson(response, e.getMessage());
            logger.error(e.getMessage(), e);
        }
    }
    /**
     * 根据角色ID查询权限。
     *
     * @param roleId
     * TA清算
     * @param request
     * @param response
     */
    @RequestMapping(value = "/byRoleId/{roleId}", method = RequestMethod.GET)
    public void byRoleId(@PathVariable Integer roleId, HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("================== sysRole query ==================");
            RoleOperateListVO roleOperateListVO = sysRoleService.getRoleOperatesById(roleId);
            //根据角色id 查询具体权限信息
            Map queryListByTypeAndRoleId = sysRoleService.queryListByTypeAndRoleId(roleOperateListVO);

            HtmlUtil.writerSuccessJson(response, queryListByTypeAndRoleId);
        } catch (CustomException eme) {
            HtmlUtil.writeFallJsonByKey(request, response, eme.getMessage(), null);
            logger.debug(eme.getMessage());
        } catch (Exception e) {
            HtmlUtil.writerFailJson(response, e.getMessage());
            logger.error(e.getMessage(), e);
        }
    }
}
